package com.mulanweb.api.module.controller.wechat;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.controller.BaseController;
import com.mulanweb.api.module.model.*;
import com.mulanweb.db.common.Constant;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.handler.wechat.UserHandler;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/26.
 */
@ValidateFile
public class CollectionController extends BaseController {

    public void search() {
        String openId = getSessionAttr("openId");//"oULNBwOvt_D2A_8se1bQOQ06ky0w";//
        if (UserHandler.userNotNull(openId)) {
            User user = User.dao.findFirst("SELECT * FROM `user` WHERE open_id = ?",openId);
            SQLGenerateHelper helper = new SQLGenerateHelper("collection", "create_time desc");
            helper.addEqualsCondition("user_id", user.getId());
            Page<Collection> page = Collection.dao.paginate(getPageNumber(), getPageSize(), helper.getSqlSelect(), helper.getSqlExceptSelect(), helper.getQueryParas());
            Page<Map<String,Object>> resultPages = new Page<>(new ArrayList<>(), page.getPageNumber(), page.getPageNumber(), page.getTotalPage(), page.getTotalRow());
            for (Collection cc : page.getList()) {
                if (cc.getTypeId() == 23) {
                    Ticket ticket = Ticket.dao.findById(cc.getTargetId());
                    //type_id = 14  表示查询门票券票logo图片 详情请查看photo表
                    Map<String,Object> ticketMap = new HashMap<String,Object>();
                    ticketMap.put("content",ticket);
                    ticketMap.put("cityName",City.dao.findById(ticket.getCityId()).getNameCn());
                    resultPages.getList().add(ticketMap);
                } else if (cc.getTypeId() == 24) {
                    Chartered chartered= Chartered.dao.findById(cc.getTargetId());
                    Map<String,Object> charteredMap = new HashMap<String,Object>();
                    charteredMap.put("content",chartered);
                    charteredMap.put("cityName",City.dao.findById(chartered.getCityId()).getNameCn());
                    resultPages.getList().add(charteredMap);
                }
            }
            renderJson(resultPages);
        } else {
            returnJson(0, Constant.PromptMessage.NOT_EXIST_USER);
        }
    }

}
